
AddCSLuaFile( "shared.lua" )

SWEP.Author			= ""
SWEP.Contact		= ""
SWEP.Purpose		= ""
SWEP.Instructions	= ""

if ( CLIENT ) then

SWEP.PrintName			= "Fretta Base"
SWEP.Slot				= 0	
SWEP.SlotPos			= 1	
SWEP.DrawAmmo			= false	
SWEP.DrawCrosshair		= true 
SWEP.DrawWeaponInfoBox	= false	
SWEP.BounceWeaponIcon   = false	
SWEP.SwayScale			= 1.0
SWEP.BobScale			= 1.0
SWEP.WepSelectIcon		= surface.GetTextureID( "weapons/swep" )

end

SWEP.DrawViewModel	= true
SWEP.ViewModelFOV	= 62
SWEP.ViewModelFlip	= false
SWEP.ViewModel		= "models/weapons/v_pistol.mdl"
SWEP.WorldModel		= "models/weapons/w_pistol.mdl"
SWEP.AnimPrefix		= "pistol"

SWEP.MovementInnacuracyOn				= true
SWEP.MovementInnacuracyMin 				= 100
SWEP.MovementInnacuracyMax 				= 200

SWEP.UseCustomCrosshair					= true
SWEP.AccurateCrosshair 					= true
SWEP.CustomCrosshairGap				 	= 10
SWEP.CustomCrosshairLength			 	= 10
SWEP.CustomCrosshairExpandShootTime 	= 0.5
SWEP.CustomCrosshairExpandShootSize		= 10
SWEP.CustomCrosshairExpandAccuracySize	= 20


SWEP.Spawnable				= false
SWEP.AdminSpawnable			= false

SWEP.Primary.ClipSize		= 100				// Size of a clip
SWEP.Primary.DefaultClip	= 100				// Default number of bullets in a clip
SWEP.Primary.Automatic		= true				// Automatic/Semi Auto
SWEP.Primary.Ammo			= "pistol"			// This is only really used for ammo display
SWEP.Primary.ConsumePAmmo	= 1
SWEP.Primary.ConsumeSAmmo	= 0
SWEP.Primary.PDelay			= 0.3
SWEP.Primary.SDelay			= 0.0
SWEP.Primary.Sound			= Sound( "Weapon_AK47.Single" )
SWEP.Primary.Recoil			= 1
SWEP.Primary.ConeBest		= 0
SWEP.Primary.ConeWorst		= 0.2
SWEP.Primary.NumShots		= 1
SWEP.Primary.Damage			= 10
SWEP.Primary.PhysForce		= 1
SWEP.Primary.TracerName		= nil
SWEP.Primary.HullSize		= 0

SWEP.Secondary.ClipSize		= 100				// Size of a clip
SWEP.Secondary.DefaultClip	= 100				// Default number of bullets in a clip
SWEP.Secondary.Automatic	= true				// Automatic/Semi Auto
SWEP.Secondary.Ammo			= "none"
SWEP.Secondary.ConsumePAmmo	= 0
SWEP.Secondary.ConsumeSAmmo	= 1
SWEP.Secondary.PDelay		= 0.0
SWEP.Secondary.SDelay		= 0.3
SWEP.Secondary.Sound		= Sound( "Weapon_AK47.Single" )
SWEP.Secondary.Recoil		= 1
SWEP.Secondary.ConeBest		= 0
SWEP.Secondary.ConeWorst	= 0.2
SWEP.Secondary.NumShots		= 1
SWEP.Secondary.Damage		= 10
SWEP.Secondary.PhysForce	= 1
SWEP.Secondary.TracerName	= nil
SWEP.Secondary.HullSize		= 0

/*---------------------------------------------------------
   Name: SWEP:DoAttack( inf )
---------------------------------------------------------*/
function SWEP:DoAttack( inf )

	if ( inf.ConsumePAmmo > 0 && !self:CanPrimaryAttack() ) then return end
	if ( inf.ConsumeSAmmo > 0 && !self:CanSecondaryAttack() ) then return end
	
	if ( inf.PDelay && inf.PDelay > 0 ) then self.Weapon:SetNextPrimaryFire( CurTime() + inf.PDelay ) end
	if ( inf.SDelay && inf.SDelay > 0  ) then self.Weapon:SetNextSecondaryFire( CurTime() + inf.SDelay ) end
	
	self.Weapon:EmitSound( inf.Sound )

	self:ShootBullet( inf.Damage, inf.NumShots, self:GetConeSize( inf.ConeBest, inf.ConeWorst ), inf.PhysForce, inf.TracerName, inf.HullSize )
	
	if ( self.Owner:IsNPC() ) then return end
	
	self.Owner:ViewPunch( Angle( math.Rand(-0.2,-0.1) * inf.Recoil, math.Rand(-0.1,0.1) * inf.Recoil, 0 ) )
	
	if ( (SinglePlayer() && SERVER) || CLIENT ) then
		self.Weapon:SetNetworkedFloat( "LastShootTime", CurTime() )
	end
	
	if ( inf.ConsumePAmmo ) then self:TakePrimaryAmmo( inf.ConsumePAmmo ) end
	if ( inf.ConsumeSAmmo ) then self:TakeSecondaryAmmo( inf.ConsumeSAmmo ) end

end

/*---------------------------------------------------------
   Name: SWEP:PrimaryAttack( )
   Desc: +attack1 has been pressed
---------------------------------------------------------*/
function SWEP:PrimaryAttack()

	self:DoAttack( self.Primary )
	
end


/*---------------------------------------------------------
   Name: SWEP:SecondaryAttack( )
   Desc: +attack2 has been pressed
---------------------------------------------------------*/
function SWEP:SecondaryAttack()

	self:DoAttack( self.Secondary )

end

/*---------------------------------------------------------
   Name: SWEP:ShootBullet( )
   Desc: A convenience function to shoot bullets
---------------------------------------------------------*/
function SWEP:ShootEffects()

	self.Weapon:SendWeaponAnim( ACT_VM_PRIMARYATTACK ) 		// View model animation
	self.Owner:MuzzleFlash()								// Crappy muzzle light
	self.Owner:SetAnimation( PLAYER_ATTACK1 )				// 3rd Person Animation

end

/*---------------------------------------------------------
   Name: GetConeSize, based on accuracy
---------------------------------------------------------*/
function SWEP:GetConeSize( cmin, cmax )

	local scale = self:GetAccuracyScale()
	
	return Lerp( scale, cmin, cmax )
end

/*---------------------------------------------------------
   Name: SWEP:ShootBulletEx( )
   Desc: A convenience function to shoot bullets
---------------------------------------------------------*/
function SWEP:ShootBulletEx( damage, num_bullets, aimcone, tracerchance, physforce, tracername, hullsize )
	
	local bullet = {}
	bullet.Num 		= num_bullets
	bullet.Src 		= self.Owner:GetShootPos()			// Source
	bullet.Dir 		= self.Owner:GetAimVector()			// Dir of bullet
	bullet.Spread 	= Vector( aimcone, aimcone, 0 )		// Aim Cone
	bullet.Tracer	= tracerchance						// Show a tracer on every x bullets 
	bullet.Force	= physforce							// Amount of force to give to phys objects
	bullet.Damage	= damage
	bullet.HullSize = hullsize
	bullet.TracerName = tracername
	
	self.Owner:FireBullets( bullet )
	
	self:ShootEffects()
	
end

/*---------------------------------------------------------
   Name: SWEP:ShouldDrawViewModel( )
---------------------------------------------------------*/
function SWEP:ShouldDrawViewModel()

	return self.DrawViewModel

end

/*---------------------------------------------------------
   Returns a number between 0-1 representing how inaccurate it should be due to movement
---------------------------------------------------------*/
function SWEP:GetMovementInnacuracy()

	if ( !self.MovementInnacuracyOn ) then return 0 end
	if ( !IsValid( self.Owner ) ) then return 0 end
	
	local Speed = self.Owner:GetVelocity():Length()
	//print( Speed )
	if ( Speed < self.MovementInnacuracyMin ) then return 0 end
	if ( Speed > self.MovementInnacuracyMax ) then return 1 end
	
	return (Speed - self.MovementInnacuracyMin) / ( self.MovementInnacuracyMax - self.MovementInnacuracyMin )

end

function SWEP:GetAccuracyScale()

	local scale = self:GetMovementInnacuracy()
	
	return scale

end


function SWEP:DoDrawCrosshair( x, y )

	if ( !self.UseCustomCrosshair ) then return end

	local LastShot = self:LastShootTime()	
	LastShot = (1 - math.Clamp( (CurTime() - LastShot) * ( 1 / self.CustomCrosshairExpandShootTime ), 0, 1 ))
	
	local accscale = self:GetAccuracyScale()
	
	local pullapart = (LastShot * self.CustomCrosshairExpandShootSize) + (accscale * self.CustomCrosshairExpandAccuracySize)

	self:PaintCrosshair( x, y, pullapart )
	
	return true

end

// This function is designed to be overridden, 
// if you want to render your own crosshair - or change the sizes
function SWEP:PaintCrosshair( x, y, pullapart )

	surface.SetDrawColor( 255, 255, 255, 255 )
	
	pullapart = pullapart + self.CustomCrosshairGap
	
	surface.DrawRect( x - pullapart - self.CustomCrosshairLength, y, self.CustomCrosshairLength, 1 ) 
	surface.DrawRect( x + pullapart + 2, y, self.CustomCrosshairLength, 1 ) 
	surface.DrawRect( x, y - pullapart - self.CustomCrosshairLength, 1, self.CustomCrosshairLength ) 
	surface.DrawRect( x, y + pullapart + 2, 1, self.CustomCrosshairLength ) 
	
end

/*---------------------------------------------------------
   Name: GetTracerOrigin()
---------------------------------------------------------*/
function SWEP:GetTracerOrigin( ply )

	if ( self.DrawViewModel ) then return end
	
	return self:GetOwner():EyePos()

end
